package Plugin_Reprise.launcher;

import java.awt.Font;
import java.awt.Image;
import java.io.File;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class Tabgroup2 extends AbstractLaunchConfigurationTab {
	public Text fProgramText=null;
	  private TreeViewer viewer;

	@Override
	public void createControl(Composite parent) {

		org.eclipse.swt.graphics.Font font = parent.getFont();
		

	    
		Composite comp = new Composite(parent, SWT.NONE);
		setControl(comp);
	    FillLayout fillLayoutsh2 = new FillLayout();
	    fillLayoutsh2.type = SWT.VERTICAL;
	    comp.setLayout(fillLayoutsh2);
	    GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
	    comp.setLayoutData(data);
		
		comp.setLayout(fillLayoutsh2);	  
		viewer = new TreeViewer(comp, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
	    
	    viewer.setContentProvider(new ViewContentProvider());
	    viewer.setLabelProvider(new ViewLabelProvider());
		IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
	    viewer.setInput(projects);
	    
	    Label label2 = new Label(parent, SWT.NONE);
	    label2.setText("Liste des taches");
	    /*Text text2 = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
	    text2.setSize(200, 10);
	    text2.setText("           ");
	    data = new GridData();
	    data.widthHint = 600;
	    data.heightHint = 60;
	    text2.setLayoutData(data);*/

	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "test de tab";
	}

	@Override
	public void initializeFrom(ILaunchConfiguration arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void performApply(ILaunchConfigurationWorkingCopy arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setDefaults(ILaunchConfigurationWorkingCopy arg0) {
		// TODO Auto-generated method stub

	}
	class ViewContentProvider implements ITreeContentProvider {
	    public void inputChanged(Viewer v, Object oldInput, Object newInput) {
	    }

	    @Override
	    public void dispose() {
	    }

	    @Override
	    public Object[] getElements(Object inputElement) {

	      return (IResource[]) inputElement;
	    }

	    @Override
	    public Object[] getChildren(Object parentElement) {
 	
			try {
	    	
		    	IResource res = (IResource) parentElement;
		    	if (res.getType()==IResource.PROJECT) return ((IProject) parentElement).members();
		    	if (res.getType()==IResource.FOLDER) return ((IFolder) parentElement).members();
		    	if (res.getType()==IResource.FILE) return null;
		    	
			} catch (CoreException e) {
				e.printStackTrace();
			}
	      return null;
	    }

	    @Override
	    public Object getParent(Object element) {
	    	IResource file = (IResource) element;
	      return file.getParent();
	    }

	    @Override
	    public boolean hasChildren(Object element) {
	    	IResource res = (IResource) element;
			try {
		    	if (res.getType()==IResource.PROJECT && ((IProject) element).members().length>0) 
		    		return true;
		    	if (res.getType()==IResource.FOLDER && ((IFolder) element).members().length>0) 
		    		return true;
		    	if (res.getType()==IResource.FILE) 
		    		return false;
			} catch (CoreException e) {
				e.printStackTrace();
			}
		    return false;
		 }

	  }


	
	class ViewLabelProvider extends LabelProvider {
	    @Override
	    public String getText(Object element) {
	      IResource file = (IResource) element;
	      String name = file.getName();
	      return name;
	    }

	    public org.eclipse.swt.graphics.Image getImage(Object obj) {
	      return null;
	    }
	  }

	  //@Focus
	  public void setFocus() {
	    viewer.getControl().setFocus();
	  }
	  
	  //@PreDestroy
	  public void dispose() {
	    //image.dispose();
	  }

	
}
